# Contributor: Mehdi Chinoune <mehdi.chinoune@hotmail.com>

_realname=viskores
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=1.0.0
pkgrel=3
pkgdesc="A visualization library for many-threaded devices. (mingw-w64)"
arch=('any')
mingw_arch=('ucrt64' 'clang64' 'clangarm64')
url='https://github.com/Viskores/viskores'
license=('spdx:BSD-3-Clause')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-hdf5"
         "${MINGW_PACKAGE_PREFIX}-omp"
         "${MINGW_PACKAGE_PREFIX}-tbb")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             #"${MINGW_PACKAGE_PREFIX}-anari-sdk"
             "${MINGW_PACKAGE_PREFIX}-glew")
#optdepends=("${MINGW_PACKAGE_PREFIX}-anari-sdk")
source=("https://github.com/Viskores/viskores/archive/v${pkgver}/${_realname}-${pkgver}.tar.gz")
sha256sums=('5bff5bbd747b7662bb4630889960371d06fcc5e5a962d974a898d1883f196eba')

build() {
  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    cmake \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      -DCMAKE_DLL_NAME_WITH_SOVERSION=ON \
      "${extra_config[@]}" \
      -DBUILD_SHARED_LIBS=ON \
      -DViskores_INSTALL_CMAKE_MODULE_DIR=lib/cmake \
      -DViskores_CUSTOM_LIBRARY_SUFFIX="" \
      -DViskores_ENABLE_DEVELOPER_FLAGS=OFF \
      -DViskores_ENABLE_GL_CONTEXT=ON \
      -DViskores_ENABLE_ANARI=OFF \
      -DViskores_ENABLE_HDF5_IO=ON \
      -DViskores_ENABLE_OPENMP=ON \
      -DViskores_ENABLE_TBB=ON \
      -DViskores_ENABLE_TESTING=OFF \
      -DViskores_USE_DEFAULT_TYPES_FOR_VTK=ON \
      -S "${_realname}-${pkgver}" \
      -B "build-${MSYSTEM}"

  cmake --build "build-${MSYSTEM}"
}

package() {
  DESTDIR="${pkgdir}" cmake --install "build-${MSYSTEM}"

  install -Dm644 "${srcdir}"/${_realname}-${pkgver}/LICENSE.txt \
    "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE

  # Fix CMake config file
  BUILD_DIR=$(cygpath -m "${srcdir}/build-${MSYSTEM}")
  sed -e "s|${BUILD_DIR}|"A:/_"|g" \
    -i "${pkgdir}"${MINGW_PREFIX}/lib/cmake/viskores-${pkgver%.*}/ViskoresConfig.cmake
}
